package com.cake.base.exception;

import java.util.zip.DataFormatException;

import javax.management.modelmbean.XMLParseException;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常处理器
 * 
 * @ClassName: GlobalExceptionHandler
 * @Description:
 */
@ControllerAdvice
public class GlobalExceptionHandler {

	private static Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	/**
	 * 类的字节码未找到异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = ClassNotFoundException.class)
	public @ResponseBody Object classNotFoundExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----类的字节码未找到" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
	/**
	 * 日期格式化异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = DataFormatException.class)
	public @ResponseBody Object dataFormatExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----日期格式化异常" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
	/**
	 * 类中无此字段异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = NoSuchFieldException.class)
	public @ResponseBody Object noSuchFieldExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----类中无此字段异常" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
	/**
	 * 类中无此方法异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = NoSuchMethodException.class)
	public @ResponseBody Object noSuchMethodExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----类中无此方法异常" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
	/**
	 * xml解析异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = XMLParseException.class)
	public @ResponseBody Object xmlParseExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----xml解析异常" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
	/**
	 * 类型转换异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = ClassCastException.class)
	public @ResponseBody Object classCastExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----类型转换异常" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
	/**
	 * 非法参数异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = IllegalArgumentException.class)
	public @ResponseBody Object illegalArgumentExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----非法参数异常" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
	/**
	 * 数组越界异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = IndexOutOfBoundsException.class)
	public @ResponseBody Object indexOutOfBoundsExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----数组越界异常" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
	/**
	 * 空指针异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = NullPointerException.class)
	public @ResponseBody Object nullPointerExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----空指针异常" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
	/**
	 * 数组非法值异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = ArrayStoreException.class)
	public @ResponseBody Object arrayStoreExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----数组非法值异常" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
	/**
	 * 其他类型异常
	 * @param request
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = Exception.class)
	public @ResponseBody Object otherExceptionHandler(HttpServletRequest request, Exception e) {
		e.printStackTrace();
		logger.info("-----其他类型异常" + e);
		logger.info(request.getRequestURI());
		return null;
	}
	
}
